﻿#include <QMessageBox>
#include <QSettings>
#include <QTreeWidgetItem>
#include "ui_MainWindow.h"
#include "MainWindow.h"
#include "../DatabaseMgmt/UserInfo.h"


CMainWindow::CMainWindow(QWidget* parent /*= Q_NULLPTR*/, Qt::WindowFlags flags /*= Qt::WindowFlags()*/)
	: QMainWindow(parent, flags)
	, m_ui(nullptr)
{
	init();
}

CMainWindow::~CMainWindow()
{

}

void CMainWindow::init()
{
	m_ui = new Ui::MainWindow();

	m_ui->setupUi(this);

	show_login();

	updateSaveLogin();

	connect(m_ui->btnLogin, SIGNAL(clicked()), this, SLOT(slot_login()));

	connect(m_ui->frameChecked, SIGNAL(sigClicked()), this, SLOT(slotItem1()));
	connect(m_ui->frameComingSone1, SIGNAL(sigClicked()), this, SLOT(slotItem2()));
	connect(m_ui->frameComingSone2, SIGNAL(sigClicked()), this, SLOT(slotItem3()));
	connect(m_ui->frameDataSet, SIGNAL(sigClicked()), this, SLOT(slotItem4()));

	connect(m_ui->pageJudge, SIGNAL(sigHome()), this, SLOT(slotGoHome()));
	connect(m_ui->pageSettings, SIGNAL(sigHome()), this, SLOT(slotGoHome()));
	connect(m_ui->pageImage, SIGNAL(sigHome()), this, SLOT(slotGoHome()));
	connect(m_ui->pageSample, SIGNAL(sigHome()), this, SLOT(slotGoHome()));

	m_ui->lblLogo->setFixedSize(64, 64);
	m_ui->frameLogin->setFixedSize(360, 500);
}

void CMainWindow::show_login()
{
	m_ui->statusbar->showMessage(QStringLiteral("等待登陆"));
}

void CMainWindow::show_mainwindow()
{
	m_ui->stackedWidget->setCurrentIndex(WT_Work);
}

void CMainWindow::slotItem1()
{
	m_ui->stackedWidget->setCurrentIndex(WT_DataJudge);
}

void CMainWindow::slotItem2()
{
	m_ui->stackedWidget->setCurrentIndex(WT_Waiting1);
}

void CMainWindow::slotItem3()
{
	m_ui->stackedWidget->setCurrentIndex(WT_Waiting2);
}

void CMainWindow::slotItem4()
{
	m_ui->stackedWidget->setCurrentIndex(WT_BasisSettings);
}

void CMainWindow::slotGoHome()
{
	m_ui->stackedWidget->setCurrentIndex(1);
}

void CMainWindow::slotGoLogin()
{
	m_ui->stackedWidget->setCurrentIndex(0);
}

void CMainWindow::slot_login()
{
	if (!checkedLoginValid())
	{
		return;
	}

	CUserInfo usrinfo;
	int ret = usrinfo.checkeUserInfo(m_ui->leAccount->text(), m_ui->lePassword->text());

	if (ret)
	{
		if (ret == CONST_ERR_NON_EXISTENT)
		{
			QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("该用户不存在！"));
		}
		else if (ret == CONST_ERR_PASSWORD_INVALID)
		{
			QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("密码输入错误！"));
		}
		
	}
	else
	{
		if (m_ui->ckbRember->isChecked())
		{
			saveLoginUser();
		}
		else
		{
			removeLoginUser();
		}
		show_mainwindow();
	}
}

bool CMainWindow::checkedLoginValid()
{
	bool bValid = true;

	if (m_ui->leAccount->text().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("请输入账户名！"));
		bValid = false;
	}
	else if (m_ui->lePassword->text().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("请输入密码！"));
		bValid = false;
	}
	else if (m_ui->leVerificationCode->text().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("请输入验证码！"));
		bValid = false;
	}
	else if (m_ui->leVerificationCode->text().toUpper() != m_ui->lblVerification->getVerificationCode())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("验证码无效！"));
		bValid = false;
	}
	return bValid;
}

void CMainWindow::saveLoginUser()
{
	QSettings settings("user.ini", QSettings::IniFormat);

	settings.setValue("user/account", m_ui->leAccount->text());
	QByteArray base64 = m_ui->lePassword->text().toLocal8Bit().toBase64();
	settings.setValue("user/verification", QString::fromLocal8Bit(base64));
}

void CMainWindow::removeLoginUser()
{
	QFile file("user.ini");
	if (file.exists())
	{
		bool bbb = file.remove();
		bbb = 1;
	}
}

void CMainWindow::updateSaveLogin()
{
	QFile file("user.ini");
	if (file.exists())
	{
		m_ui->ckbRember->setChecked(true);
		QSettings settings("user.ini", QSettings::IniFormat);
		m_ui->leAccount->setText(settings.value("user/account").toString());
		QString strPassword = settings.value("user/verification").toString();
		strPassword = QByteArray::fromBase64(strPassword.toLocal8Bit());
		m_ui->lePassword->setText(strPassword);
	}
}




